package com.demo.user.service.handler.transfer;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author : YU.TAN
 * @date : 2021/8/27 13:49
 */
@Slf4j
@Service
public class TransferExecute {
    private final List<TransferProcess> transferProcesses;

    public TransferExecute(List<TransferProcess> transferProcesses) {
        this.transferProcesses = transferProcesses;
    }

    @Transactional(rollbackFor = Exception.class)
    @SneakyThrows
    public void execute(Long oldUserId, Long newUserId) {
        for (TransferProcess transferProcess : transferProcesses) {
            List<Long> ids = transferProcess.load(oldUserId);
            if (CollectionUtils.isEmpty(ids)) {
                continue;
            }
            log.info("数据查询完毕，准备执行！查询到数据id集合：{}", ids);
            transferProcess.transfer(ids, newUserId);
            log.info("执行完毕！");
        }
    }
}
